
#include "nixie.h"
#include "timer2.h"
#include "key.h"
#include "led.h"
#include "AT24C02.h"
#include "i2c.h"
#include "Delay.h"

unsigned char KeyNumber = 0;
unsigned char S_B = 0;
static unsigned int count2 = 400;
static unsigned int TIME = 400;
unsigned char Mode = 1;
unsigned char blink = 0;
unsigned char calss = 0;

unsigned char rb2(void)
{
	unsigned int RB2 = 0;
	unsigned char Calss = 0;
	RB2 = ad_read(0x43);
	Delay(5);
	da_write(5*(RB2/255.0));
	Delay(5);
//	Nixie_show(1,5*(RB2/255.0));
	if((RB2/255.0)<=0.25)
		Calss = 1;
	if((RB2/255.0)>0.25 && (RB2/255.0)<=0.5)
		Calss = 2;
	if((RB2/255.0)>0.5 && (RB2/255.0)<=0.75)
		Calss = 3;
	if((RB2/255.0)>0.75 && (RB2/255.0)<=1)
		Calss = 4;
	return Calss;
}

unsigned int time_return(void)
{
		unsigned char time_h = 0;
		unsigned char time_l = 0;
		unsigned int time = 0x00;
		time_h = AT24C02_Read(1);
		time_l = AT24C02_Read(2);
		time = time_h;
		time <<= 8;
		time |= time_l; 
	return time;
}

void set(unsigned char mode,unsigned int time)
{
	
	if(time>=1000)
	{
			Nixie_show(5,(time)/1000);
			Nixie_show(6,(time)/100%10);
			Nixie_show(7,(time)/10%10);
			Nixie_show(8,(time)%10);
	}
	else
		if(time<1000)
		{
			Nixie_show(5,20);
			Nixie_show(6,(time)/100%10);
			Nixie_show(7,(time)/10%10);
			Nixie_show(8,(time)%10);
		}
	Nixie_show(3,21);
	Nixie_show(1,21);
	Nixie_show(2,mode);
	
}

void set_time(unsigned int time)
{
	if(!blink)
	{
	if(time>=1000)
	{
			Nixie_show(5,(time)/1000);
			Nixie_show(6,(time)/100%10);
			Nixie_show(7,(time)/10%10);
			Nixie_show(8,(time)%10);
	}
	else
		if(time<1000)
		{
			Nixie_show(5,20);
			Nixie_show(6,(time)/100%10);
			Nixie_show(7,(time)/10%10);
			Nixie_show(8,(time)%10);
		}
	}
	if(blink)
	{
			Nixie_show(5,20);
			Nixie_show(6,20);
			Nixie_show(7,20);
			Nixie_show(8,20);
	}
	blink = !blink;
}

void time_set(unsigned int* time)
{
	static unsigned int time_count = 0;
	while(1)
	{
		KeyNumber = r_key();
		if(KeyNumber == 4)
			S_B = !S_B;
		if(KeyNumber == 3)
		{
			blink = 0;
			set_time(*time);
			AT24C02_Write(1,(*time)>>8);
			AT24C02_Write(2,(*time&0xff));
			break;	
		}
		if(KeyNumber == 2)
		{
			if(*time<=1100)
			{
				*time += 100;
				set_time(*time);
			}
		}
		if(KeyNumber == 1)
		{
			if(*time >= 500)
			{
				*time -= 100;
				set_time(*time);
			}
		}
		time_count++;
		if(time_count == 4500)
		{
			 set_time(*time);
		}
	}
}

void mode_set(unsigned char* mode)
{
	static unsigned int mode_count = 0;
	while(1)
	{
		KeyNumber = r_key();
		if(KeyNumber == 4)
			S_B = !S_B;
	if(KeyNumber == 2)
		if(*mode<=3)
		{
			*mode += 1;
				if(!blink)
						{
							Nixie_show(2,*mode);
						}
						if(blink)
						{
							Nixie_show(2,20);
						}
						blink = !blink;
						}
			if(KeyNumber == 1)
				if(*mode>=2)
				{
					*mode -= 1;
					if(!blink)
						{
							Nixie_show(2,*mode);
						}
						if(blink)
						{
							Nixie_show(2,20);
						}
						blink = !blink;
				}
					if(KeyNumber == 3)
					{
						Nixie_show(2,*mode);
						AT24C02_Write(3,*mode);
						break;		
					}
					mode_count++;
					if(mode_count == 4500)
					{
						if(!blink)
						{
							Nixie_show(2,*mode);
						}
						if(blink)
						{
							Nixie_show(2,20);
						}
						blink = !blink;
					}
				}
}



void Time_mode_Set()
{
	static unsigned char mode = 0;
	static unsigned int time = 0;
	unsigned char i = 0;
	mode = Mode;
	time = TIME;
	set(mode,time);
	while(1)
	{
		mode_set(&mode);
		Mode = AT24C02_Read(3);
		time_set(&time);
		TIME = time_return();
		for(i = 1;i<=8;i++)
		{
			Nixie_show(i,20);
		}
		break;
	}
}

void main()
{
	Latch(RELAY,0x00);
	Latch(LED,0xFF);
	TIME = time_return();
	count2 = TIME;
	Mode = AT24C02_Read(3);
	Timer2Init();
	while(1)
	{
		KeyNumber = r_key();
		calss = rb2();
		if(KeyNumber == 4)
			S_B = !S_B;
		if(KeyNumber == 3)
			Time_mode_Set();
		if(Key_1 == 0)
		{
			while(Key_1 == 0)
			{
				Nixie_show(7,21);
				Nixie_show(8,calss);
			}
		}
			Nixie_show(7,20);
			Nixie_show(8,20);
	}
}


void Timer2()interrupt 12 using 1
{
	static unsigned char count1 = 0;
	T2L = 0xCD;		//设置定时初值
	T2H = 0xD4;		//设置定时初值
	if(++count1 == 20)
	{
		count1 = 0;
		key_loop();
	}
	if(S_B)
	{
	if(--count2 == 0)
	{
		count2 = TIME;
		if(Mode == 1)
			led_1();
		if(Mode == 2)
			led_2();
		if(Mode == 3)
			led_3();
		if(Mode == 4)
			led_4();
	}
	}
	N_loop();
}